package com.example.speeddating.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.speeddating.constants.TaoHuaConstant;
import com.example.speeddating.entity.TaohuaDetail;
import com.example.speeddating.mapper.ArticleMapper;
import com.example.speeddating.mapper.CommentMapper;
import com.example.speeddating.mapper.LikeMapper;
import com.example.speeddating.mapper.TaohuaDetailMapper;
import com.example.speeddating.service.TaohuaDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:34
 */
@Slf4j
@Service
public class TaohuaDetailServiceImpl extends ServiceImpl<TaohuaDetailMapper, TaohuaDetail> implements TaohuaDetailService {

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ArticleMapper articleMapper;


    @Autowired
    private TaohuaDetailMapper taohuaDetailMapper;

    @Override
    public Page<List<TaohuaDetail>> getTaohuaDetail(Page<TaohuaDetail> detailPage,long userId) {
        Page<TaohuaDetail> list = taohuaDetailMapper.getList(detailPage, userId);

        List<TaohuaDetail> records = list.getRecords();
        ArrayList<List<TaohuaDetail>> arrayList = new ArrayList<>();

        for (TaohuaDetail record : records) {
            log.info(record.toString());
            List<TaohuaDetail> listByYearMonth = taohuaDetailMapper.getListByYearMonth(record.getYear(), record.getMonth(), userId);
            arrayList.add(listByYearMonth);
        }
        log.info(arrayList.stream().toArray().toString());

        //处理返回后的数据
        Page<List<TaohuaDetail>> listPage = new Page<>();
        listPage.setTotal(list.getTotal());
        listPage.setPages(list.getPages());
        listPage.setSize(list.getSize());
        listPage.setCurrent(list.getCurrent());
        listPage.setRecords(arrayList);
        return listPage;
    }

    @Override
    public void insertTaohuaDetail(Integer type,long userId) {
        TaohuaDetail taohuaDetail = new TaohuaDetail();
        if (type==TaoHuaConstant.Type_1){
            taohuaDetail.setUserId(userId);
            taohuaDetail.setCount(TaoHuaConstant.LOGIN_COUNT);
            taohuaDetail.setRemark(TaoHuaConstant.Type1_REMARK);
        }else if (type==TaoHuaConstant.Type_2){
            if (likeMapper.selectIsLikeInToday(userId)==0){
                taohuaDetail.setUserId(userId);
                taohuaDetail.setCount(TaoHuaConstant.LICK_COUNT);
                taohuaDetail.setRemark(TaoHuaConstant.Type2_REMARK);
            }
        } else if (type==TaoHuaConstant.Type_3){
            if (commentMapper.selectIsCommentInToday(userId)==0){
                taohuaDetail.setUserId(userId);
                taohuaDetail.setCount(TaoHuaConstant.COMMENT_COUNT);
                taohuaDetail.setRemark(TaoHuaConstant.Type3_REMARK);
            }
        } else if (type==TaoHuaConstant.Type_4){
            if (articleMapper.selectIsPublishInToday(userId)==0){
                taohuaDetail.setUserId(userId);
                taohuaDetail.setCount(TaoHuaConstant.PUBLISH_COUNT);
                taohuaDetail.setRemark(TaoHuaConstant.Type4_REMARK);
            }
        }else if (type==TaoHuaConstant.Type_5){
            taohuaDetail.setUserId(userId);
            taohuaDetail.setCount(TaoHuaConstant.SHARE_COUNT);
            taohuaDetail.setRemark(TaoHuaConstant.Type5_REMARK);
        }else if (type==TaoHuaConstant.Type_6){
            taohuaDetail.setUserId(userId);
            taohuaDetail.setCount(TaoHuaConstant.COMPLETE_COUNT);
            taohuaDetail.setRemark(TaoHuaConstant.Type6_REMARK);
        }else if (type==TaoHuaConstant.Type_7){
            taohuaDetail.setUserId(userId);
            taohuaDetail.setCount(TaoHuaConstant.AUTHOR_COUNT);
            taohuaDetail.setRemark(TaoHuaConstant.Type7_REMARK);
        }else if (type==TaoHuaConstant.Type_8){
            taohuaDetail.setUserId(userId);
            taohuaDetail.setCount(TaoHuaConstant.UNLOCK_COUNT);
            taohuaDetail.setRemark(TaoHuaConstant.Type8_REMARK);
        }else if (type==TaoHuaConstant.Type_9){
            taohuaDetail.setUserId(userId);
            taohuaDetail.setCount(TaoHuaConstant.REMOVE_VISIT_COUNT);
            taohuaDetail.setRemark(TaoHuaConstant.Type9_REMARK);
        }
//        log.info("当前的userId"+taohuaDetail.getUserId());
        if (taohuaDetail.getUserId()!=null){
            taohuaDetailMapper.insert(taohuaDetail);
        }
    }

    @Override
    public int selectTaohuaSum(Long userId) {
        return taohuaDetailMapper.selectTaohuaSum(userId);
    }
}
